// gcc test_sendfile.c -o tesend -D_GNU_SOURCE

#include <stdio.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<time.h>
#include<unistd.h>
#include <stdlib.h>
#include <sys/file.h>
#include <string.h>
#define BUF_SIZE 1024
int test_sendfile(int s, int t);
int test_readwrite(int s, int t);

int main(){
    int source = open("c_passwd.txt", O_RDWR | O_DIRECT);
    int source1 = open("c_passwd.txt", O_RDWR | O_DIRECT);
    int target = open("target.txt", O_RDWR | O_CREAT | O_DIRECT);
    int target1 = open("target1.txt", O_RDWR | O_CREAT | O_DIRECT);
    test_sendfile(source, target);
    test_readwrite(source1, target1);
    close(source);
    close(source1);
    close(target);
    close(target1);
}

int test_sendfile(int s, int t){
    int res = 0;
    clock_t start, end;
    start = clock();
    res = sendfile(t, s, NULL, 2000000000);
    end = clock();
    printf("sendfile time : %f\n", ((double)(end-start))/CLOCKS_PER_SEC);
    printf("sendfile res:%d\n", res);
    return res;
}

int test_readwrite(int s, int t){
    int res = 0, size = -1;
    clock_t start, end;
    int ret;
    unsigned char *buf;
    ret = posix_memalign((void **)&buf, 512, BUF_SIZE);
    start = clock();
    while (size != 0) {
        size = read(s, buf, BUF_SIZE);
        write(t, buf, size);
        res+=size;
    }
    
    end = clock();
    printf("readwrite time : %f\n", ((double)(end-start))/CLOCKS_PER_SEC);
    printf("readwrite res:%d\n", res);
    return res;
}